jQuery(document).ready(function() {
	
	var $slider_type = jQuery("meta[name=slider_type]").attr('content');
	var $slider_speed = jQuery("meta[name=slider_speed]").attr('content');
	var $slider_disable = jQuery("meta[name=slider_disable]").attr('content');
	
	if($slider_type == 'fading'){$slider_img_class = "div[class^='single_fading_slide']";$loadingID = '.fading_slides';}
	if($slider_type == 'tabbed'){$slider_img_class = "div[class^='single_tabbed_slide']";$loadingID = '#tabbed_slides';}
	
	if($slider_disable == 'false'){$disable_slider = false;}
	if($slider_disable == 'true'){$disable_slider = true;}
	
	
	//pause slider on video play
	jQuery('#tabbed_slides .vvqbox').mousedown(function () {
		jQuery('#webtreats_tabbed_slider').data('scrollable').stop();
	});
	
	jQuery('.fading_slides .vvqbox').mousedown(function () {
		jQuery('.slidetabs').data('slideshow').stop();
	});
	
/**
 * Slider Image Preloader
 * 
 */
	jQuery(function () {

		// grab the images
		var $slider_images = jQuery($slider_img_class+' span img');

		// image length
		var $max_slides = $slider_images.length;

		// remove them from DOM to prevent normal load
		jQuery('.rm_slider_img').remove();

		// start loading
		if($max_slides>0) {
			LoadSliderImage(0,$max_slides);
		}

	// loading function handler
	function LoadSliderImage(index,$max_slides) {

		if(index<$max_slides) {

			// add list to div
			jQuery('<span id="slider_img'+(index+1)+'"></span>').each(function() {
			   jQuery(this).appendTo(jQuery($loadingID+' .load_slider_img').eq(index));
			});

			// new image object
			var $img = new Image();
	
			// current image
			var $curr = jQuery("#slider_img"+(index+1));
	
			// load current image
			jQuery($img).load(function () {
		
				// hide it first + .hide() failed in safari
				jQuery(this).css('display','none');
		
				//add alt attr
				jQuery(this).attr({alt: ""});
	 
				// remove loading class from div and insert the image into it
				jQuery($curr).append(this);
		
				// fade it in
				jQuery(this).fadeIn(250,function() {
			
					if(index == ($max_slides-1)) {
							// remove loading div after all images loaded then start slider
							jQuery('#loading_slider').remove();
							
							if($slider_type == 'fading'){fadingStart();}
							
							if($slider_type == 'tabbed'){
								jQuery(".single_tabbed_slide").removeClass("noscript");
								jQuery("#thumbs").removeClass("noscript");
								tabbedStart();
							}
							
							
						}else{
						  // we are loading next item
						  LoadSliderImage(index+1,$max_slides);
						}
				});
		
			}).error(function () {
				// if loading error remove div
				jQuery($curr).remove();
				// try to load next item
				LoadSliderImage(index+1,$max_slides);
			}).attr('src', jQuery($slider_images[index]).attr('src')).attr('class', jQuery($slider_images[index]).attr('class'));
	   	  }
		}
	});
	
/**
 * jQuery Tools Slideshow
 * 
 */
	function fadingStart() {	
		
			jQuery(".slidetabs").tabs(".fading_slides > div.single_fading_slide", {

				// enable "cross-fading" effect
				effect: 'fade',
				fadeOutSpeed: "slow",

				// start from the beginning after the last tab
				rotate: true,
			
				onBeforeClick : function(event,index) {
									var CurrentTab = this.getCurrentPane();
										CurrentTab.next().find('.slider_content').css('display', 'none');
										CurrentTab.find('.slider_content').css('display', 'none');
								},

				onClick : function(event,index) {
									var CurrentTab = this.getCurrentPane();
										CurrentTab.find('.slider_content').css('display', 'block');
								}
			

			// use the slideshow plugin. It accepts its own configuration
			}).slideshow({clickable: false,autoplay: $disable_slider,interval: $slider_speed});
		
		jQuery('.fading_slides').css("display", "block");
	}

/**
 * jQuery Tools Tabbed Slider
 * 
 */
	function tabbedStart() {
	
			// initialize scrollable and return the programming API
			jQuery("#webtreats_tabbed_slider").scrollable({
		
				items: '#tabbed_slides',
				size: 1,
				clickable: false,
				circular: true

			// use the navigator plugin
			}).autoscroll({autoplay: $disable_slider,interval: $slider_speed}).navigator({api: true});
	}
	
});